package models;

import java.util.*;
import javax.persistence.*;

import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

import com.avaje.ebean.*;

/**
 * StopTime entity managed by Ebean
 */
@Entity  
@Table(name="stop_times")
public class StopTime extends Model {

    @Constraints.Required
    public String trip_id;
    
    @Constraints.Required
    public String arrival_time;
    
    @Constraints.Required
    public String departure_time;
    
    @Constraints.Required
    public String stop_id;
    
    @Constraints.Required
    public String stop_sequence;
    
    public String stop_headsign;
    
    public String pickup_type;
    
    public String drop_off_type;
    
    public String shape_dist_traveled;
    
    public static Model.Finder<String,StopTime> find = new Model.Finder(String.class, StopTime.class);
    
    /**
     * Retrieve all StopTimes.
     */
    public static List<StopTime> findAll() {
        return find.all();
    }
    
    /**
     * Retrieve Trip.
     */
    public Trip findTrip() {
        return Trip.find
            .where()
            .eq("trip_id", this.trip_id)
            .findUnique();
    }
    
    /**
     * Retrieve next StopTime in this trip.
     */
    public StopTime findNextStopTime() {
        String next_stop_sequence = Integer.toString(
            Integer.parseInt(this.stop_sequence) + 1
        );
        return find
            .where()
            .eq("trip_id", this.trip_id)
            .eq("stop_sequence", next_stop_sequence)
            .findUnique();
    }
    
    /**
     * Retrieve next Stop in this trip.
     */
    public Stop findNextStop() {
        String stop_id = this.findNextStopTime().stop_id;
        return Stop.find
            .where()
            .eq("stop_id", stop_id)
            .findUnique();
    }

}

